package Cantella::DJabberd::Store::Result::LoggedMessage;

use base qw/DBIx::Class/;
use metaclass 'Reaction::Meta::Class';
use Moose;

use MooseX::Types::Moose qw(Int Str);
use Reaction::Types::DateTime ();

#create role to add to result and resultset classes that disables
#update and delete operations ! only create allowed!

use aliased 'Cantella::DJabberd::Store::Result::UserJid';

use namespace::clean -except => [ 'meta' ];

has id => (is => 'ro', required => 1, isa => Int);
has message => (is => 'ro', required => 1, isa => Str);
has delivery_d => (is => 'ro', required => 1, isa => Reaction::Types::DateTime::DateTime);
has to => (is => 'ro', required => 1, isa => UserJid);
has from => (is => 'ro', required => 1, isa => UserJid);

__PACKAGE__->load_components(qw/TimeStamp Core/);
__PACKAGE__->table('logged_messages');
__PACKAGE__->add_columns
  (
   id         => {
                  data_type => "INT",
                  size      => 10,
                  default_value => undef,
                  is_nullable   => 0,
                  is_auto_increment => 1,
                  extra => { unsigned => 1 },
                 },
   from_id    => {
                  data_type => "INT",
                  size => 10,
                  is_nullable => 0,
                  is_foreign_key => 1,
                  extra => { unsigned => 1 },
                 },
   to_id      => {
                  data_type => "INT",
                  size => 10,
                  is_nullable => 0,
                  is_foreign_key => 1,
                  extra => { unsigned => 1 },
                 },
   message    => {
                  data_type => "TEXT",
                  size      => 65535,
                  default_value => "",
                  is_nullable   => 0,
                 },
   delivery_d => {
                  data_type => "DATETIME",
                  is_nullable   => 0,
                  set_on_create => 1,
                 },
  );

__PACKAGE__->set_primary_key("id");

__PACKAGE__->belongs_to(to   => UserJid, {'foreign.id' => 'self.to_id'});
__PACKAGE__->belongs_to(from => UserJid, {'foreign.id' => 'self.from_id'});

sub display_name { shift->message }


{
  my $boom = sub { confess "This is an unalterable result class; method not supported.";};
  for my $method (qw/update insert_or_update update_or_insert delete/){
    __PACKAGE__->meta->add_method($method, $boom);
  }
}

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

1;
